//
//  SearchResult.swift
//  SearchHeader
//
//  Created by 万孟超 on 2023/10/13.
//

import Foundation

class SearchResult {
    var results: [FileResult]
    
    init(results: [FileResult]) {
        self.results = results
    }
    
    func contains(_ other: String) -> Bool {
        for result in results {
            if result.frameworkName == other {
                return true
            }
        }
        return false
    }
    
    subscript (frameworkName: String) -> FileResult? {
        for result in results {
            if result.frameworkName == frameworkName {
                return result
            }
        }
        return nil
    }
    
    subscript (index: Int) -> FileResult? {
        return results[index]
    }
    
    func replace(_ other: FileResult) {
        for (idx, result) in results.enumerated() {
            if result == other {
                results.remove(at: idx)
                break
            }
        }
        results.append(other)
    }
    
    var totalCount: Int {
        var count: Int = 0
        for result in results {
            for list in result.fileList {
                count += list.errorImportArray.count
            }
        }
        return count
    }
}

class FileResult: Equatable {
    
    var frameworkName: String
    
    var fileList: [FileNameModel]
    
    init(frameworkName: String, fileList: [FileNameModel]) {
        self.frameworkName = frameworkName
        self.fileList = fileList
    }
    
    func contains(_ other: String) -> Bool {
        for file in fileList {
            if file.fileName == other {
                return true
            }
        }
        return false
    }
    
    static func == (lhs: FileResult, rhs: FileResult) -> Bool {
        return lhs.frameworkName == rhs.frameworkName
    }
    
    subscript (fileName: String) -> FileNameModel? {
        for file in fileList {
            if file.fileName == fileName {
                return file
            }
        }
        return nil
    }
    
    subscript (index: Int) -> FileNameModel? {
        return fileList[index]
    }
    
}

class FileNameModel {
    /// 文件名称
    var fileName: String
    /// 错误的导入数组
    var errorImportArray: [ErrorInfo]
    
    init(fileName: String, errorImportArray: [ErrorInfo]) {
        self.fileName = fileName
        self.errorImportArray = errorImportArray
    }
    
    subscript (index: Int) -> ErrorInfo? {
        return errorImportArray[index]
    }
}

class ErrorInfo {
    /// 所在的文件名称
    var fileName: String
    /// 所在的文件路径
    var filePath: String
    /// 在第几行
    var line: Int
    /// 错误的导入方式
    var importContent: String
    /// 正确的导入方式
    var rightImportContent: String
    
    init(fileName: String, filePath: String, line: Int, importContent: String, rightImportContent: String) {
        self.fileName = fileName
        self.filePath = filePath
        self.line = line
        self.importContent = importContent
        self.rightImportContent = rightImportContent
    }
}
